<template>
  <div>
    <van-nav-bar
      title="账号登录"
      left-text=""
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        placeholder="请输入用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
        <div class="am-flexbox-item">
          <a href="#/registe">还没有账号，去注册~</a>
        </div>
      </div>
    </van-form>
    <router-view></router-view>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },

  created () {},

  methods: {
    onClickLeft () {
      this.$router.back()
    },
    async onSubmit () {
      try {
        const res = await axios({
          url: 'http://liufusong.top:8080/user/login',
          method: 'POST',
          data: {
            username: this.username,
            password: this.password
          }
        })
        console.log(res)
        this.username = ''
        this.password = ''
        this.$toast('登录成功')
        this.$router.push('/')
        // this.$dialog
        //   .alert({
        //     message: '登录成功！'
        //   })
        //   .then(() => {
        //     this.$router.push('/')
        //   })
      } catch (err) {
        alert('请求失败！')
      }
    }
  }
}
</script>

<style scoped lang="less">
.van-nav-bar {
  background-color: #21b97a;
  /deep/.van-icon-arrow-left {
    color: #fff;
  }
  /deep/.van-nav-bar__title {
    color: #fff;
  }
}
.van-button--info {
  background-color: #21b97a;
  border: 1px solid #21b97a;
}
.am-flexbox-item {
  margin-top: 20px;
  margin-left: 100px;
  a {
    color: #666;
    font-size: 14px;
  }
}
</style>
